﻿

using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using AutoMapper;
using Oliving.Edge.AuthHelper;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IServices;
using Microsoft.Extensions.Logging;
using Oliving.Edge.Model.Dto;

namespace Oliving.Edge.Controllers
{
    [ApiController]
    [Route("api/Sys_Dict")]
    [Authorize(Permissions.Name)]
    public class Sys_DictController : OlivingControll
    {
        IMapper _mapper;
        private readonly ISys_DictServices _Sys_DictServices;
        private readonly ILogger<Sys_DictController> _logger;


        public Sys_DictController(
                IHttpContextAccessor accessor, PermissionRequirement requirement,
                IMapper mapper, ILogger<Sys_DictController> logger,
                ISys_DictServices Sys_DictServices
            ) : base(accessor, requirement)
        {
            _logger = logger;
            _mapper = mapper;
            _Sys_DictServices = Sys_DictServices;
        }



        [HttpGet]
        [AllowAnonymous]
        [Route("/api/[controller]/Get")]
        public async Task<MessageModel<Sys_DictDto>> Get(int id)
        {
            var data = new MessageModel<Sys_DictDto>();
            Sys_Dict model = await _Sys_DictServices.QueryById(id);
            if (model == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = _mapper.Map<Sys_DictDto>(model);
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }
            return data;
        }





        [HttpGet]
        [Route("/api/[controller]/GetPageList")]
        public async Task<MessageModel<PageModel<Sys_DictDto>>> GetPageList(int page = 1, int intPageSize = 20, string key = "")
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            var data = await _Sys_DictServices.QueryPage(a =>
                            true
                            , page, intPageSize, " id desc ");

            var model = _mapper.Map<PageModel<Sys_DictDto>>(data);

            return new MessageModel<PageModel<Sys_DictDto>>()
            {
                msg = "获取成功",
                success = true,
                response = model
            };
        }



        [HttpPut]
        [Route("/api/[controller]/Update")]
        public async Task<MessageModel<string>> Update([FromBody] Sys_DictDto dto)
        {
            var data = new MessageModel<string>();

            if (dto != null && dto.id > 0)
            {
                Sys_Dict model = await _Sys_DictServices.QueryById(dto.id);
                if (model != null)
                {
                    model.dict_Type = dto.dict_Type;
                    model.dict_Key = dto.dict_Key;
                    model.dict_Value = dto.dict_Value;
                    model.dict_Sort = dto.dict_Sort;
                    model.dict_CreateTime = dto.dict_CreateTime;
                    model.dict_Update_time = dto.dict_Update_time;
                    model.dict_State = dto.dict_State;
                    model.dict_Remark = dto.dict_Remark;


                    data.success = await _Sys_DictServices.Update(model);

                    if (data.success)
                    {
                        data.msg = "更新成功";
                        data.response = dto.id.ObjToString();
                    }
                }
                else
                {
                    data.success = false;
                    data.msg = "参数异常，您要更新的数据不存在";
                }
            }
            else
            {
                data.success = false;
                data.msg = "参数异常";
            }
            return data;
        }





        [HttpPost]
        [Route("/api/[controller]/Add")]
        public async Task<MessageModel<string>> Add(Sys_Dict dto)
        {
            var data = new MessageModel<string>();
            int id = await _Sys_DictServices.Add(dto);

            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
            return data;
        }




        [HttpDelete]
        [Route("/api/[controller]/Delete")]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            if (id > 0)
            {
                var userDetail = await _Sys_DictServices.QueryById(id);
                // userDetail.C_State = -1; ?????????????
                data.success = await _Sys_DictServices.Update(userDetail);
                if (data.success)
                {
                    data.msg = "删除成功";
                    data.response = userDetail?.id.ObjToString();
                }
            }
            return data;
        }





        [HttpDelete]
        [Route("/api/[controller]/BatchDelete")]
        public async Task<MessageModel<string>> BatchDelete(string ids)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(ids))
            {
                if (1 == 1)
                {
                    #region 软删除

                    int[] list = Array.ConvertAll<string, int>(ids.Split(','), s => int.Parse(s));

                    Sys_Dict model = new Sys_Dict();
                    //  model.C_State = -1;          ???????????????????
                    string whereStr = " id in (" + ids + ")";
                    List<string> colList = new List<string>();
                    //  colList.Add("C_State");    ???????????????????
                    List<string> colHL = new List<string>();

                    bool isOK = await _Sys_DictServices.Update(model, colList, colHL, whereStr);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }

                    #endregion
                }
                {
                    #region 直接删除 

                    object[] list = Array.ConvertAll<string, object>(ids.Split(','), s => int.Parse(s));

                    bool isOK = await _Sys_DictServices.DeleteByIds(list);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }
                    #endregion
                }
            }
            return data;
        }




    }
}

